import axios from "axios";
import router from '../router'

const request = axios.create({
    baseURL : "http://localhost:8080/tijian/",
    timeout: 30000
})

request.interceptors.request.use(config=>{
    config.headers['Content-Type'] = 'application/json;charset=utf-8';
    let token = localStorage.getItem('token')
    config.headers['token'] = token
    return config
},
error =>{
    console.error("request error:"+error);
    return Promise.reject(error)
});


request.interceptors.response.use(
    response=>{
        let res = response;
        // alert(res)
        if (res.data == 'NOT_LOGIN') {
            console.log('解析令牌失败，返回登录页面');
            router.push('/login')
        }
        // if(typeof res === "string"){
        //     res = res ? JSON.parse(res) : res;
        // }
        return res;
    },
    error =>{
        console.error("（request.js）request error:"+error);
        return Promise.reject(error)
    }
)

export default request;